import { Form, Input, Select } from 'antd';
import React, { forwardRef, useImperativeHandle } from 'react';
import CatalogTreeSelect from './CatalogTreeSelect';

function ProcessForm(
  { processId, parentCatalogId, form, processName, processCode, dataSourceType, isEdit = true },
  ref
) {
  const { getFieldDecorator } = form;
  let _isEdit = !!processId;
  if (!isEdit) {
    _isEdit = false;
  }

  useImperativeHandle(ref, () => form);

  return (
    <>
      {processId ? (
        <Form.Item style={{ marginBottom: 0, display: 'none' }}>
          {getFieldDecorator('processId', {
            initialValue: processId,
          })(<Input hidden />)}
        </Form.Item>
      ) : null}
      <Form.Item label="上级目录">
        {getFieldDecorator('parentCatalogId', {
          initialValue: parentCatalogId,
        })(<CatalogTreeSelect />)}
      </Form.Item>
      <Form.Item label="流程名称">
        {getFieldDecorator('processName', {
          initialValue: processName,
          rules: [
            {
              required: true,
              message: '流程名称不能为空',
            },
          ],
        })(<Input placeholder="请输入" />)}
      </Form.Item>
      <Form.Item label="流程编码">
        {getFieldDecorator('processCode', {
          initialValue: processCode,
          rules: [
            {
              required: true,
              message: '流程编码不能为空',
            },
          ],
        })(<Input placeholder="请输入" />)}
      </Form.Item>
      <Form.Item label="数据源类型">
        {getFieldDecorator('dataSourceType', {
          initialValue: dataSourceType,
          rules: [
            {
              required: true,
              message: '请确定数据源类型',
            },
          ],
        })(
          <Select disabled={_isEdit} placeholder="--请选择--">
            <Select.Option value="None">无数据源</Select.Option>
            <Select.Option value="Http">HTTP数据源</Select.Option>
          </Select>
        )}
      </Form.Item>
    </>
  );
}

export default forwardRef(ProcessForm);
